//listPoint, getPoint, delPoint, addPoint, updatePoint
import request from '@/utils/request';

let listData = [
  {
    pointId: 1,
    pointName: "东城住宅",
    pointType: "1",
    longitude: "121.473701",
    latitude: "31.230416",
    area: "140581002",
    address: "高平市东城街道",
    contact: "张主任",
    phone: "13800138001",
    createTime: "2023-01-15"
  },
  {
    pointId: 2,
    pointName: "马村镇保护区",
    pointType: "2",
    longitude: "118.157547",
    latitude: "30.137542",
    area: "140581107",
    address: "高平市马村镇",
    contact: "李科长",
    phone: "13800138002",
    createTime: "2023-02-20"
  },
  {
    pointId: 3,
    pointName: "南城湿地",
    pointType: "3",
    longitude: "116.007690",
    latitude: "29.312342",
    area: "140581003",
    address: "高平市南城街道",
    contact: "王站长",
    phone: "13800138003",
    createTime: "2023-03-10"
  },
  {
    pointId: 4,
    pointName: "东城",
    pointType: "4",
    longitude: "120.585316",
    latitude: "31.134672",
    area: "140581002",
    address: "高平市东城街道",
    contact: "赵处长",
    phone: "13800138004",
    createTime: "2023-04-05"
  },
  {
    pointId: 5,
    pointName: "东城生态区",
    pointType: "3",
    longitude: "112.938814",
    latitude: "28.987508",
    area: "140581002",
    address: "高平市东城街道",
    contact: "陈主任",
    phone: "13800138005",
    createTime: "2023-05-18"
  }
]

export const listPoint = async (query) => {
    // 模拟分页
    const { pageNum = 1, pageSize = 10, pointName, pointType } = query;
    
    // 过滤数据
    let filteredData = [...listData];
    if (pointName) {
      filteredData = filteredData.filter(item => item.pointName.includes(pointName));
    }
    if (pointType) {
      filteredData = filteredData.filter(item => item.pointType === pointType);
    }
    
    // 计算分页
    const total = filteredData.length;
    const start = (pageNum - 1) * pageSize;
    const end = start + pageSize;
    const rows = filteredData.slice(start, end);
    
    return {
        rows: rows,
        total: total
    }
}

export const getPoint = async (id) => {
  const point = listData.find(item => item.pointId == id);
  return {
    data: point || {}
  }
}

export const delPoint = async (ids) => {
  if (Array.isArray(ids)) {
    listData = listData.filter(item => !ids.includes(item.pointId));
  } else {
    listData = listData.filter(item => item.pointId != ids);
  }
  return {}
}

export const addPoint = async (data) => {
  const newId = Math.max(...listData.map(item => item.pointId), 0) + 1;
  const newData = {
    pointId: newId,
    ...data,
    createTime: new Date().toISOString().slice(0, 10)
  };
  listData.push(newData);
  return {}
}

export const updatePoint = async (data) => {
  const index = listData.findIndex(item => item.pointId === data.pointId);
  if (index !== -1) {
    listData[index] = { ...listData[index], ...data };
  }
  return {}
}